package com.yao.club.controller;

import com.netease.yidun.sdk.core.utils.StringUtils;
import com.yao.club.entity.dto.InformationType;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.entity.result.ResultEntity;
import com.yao.club.entity.vo.InformationDetailVO;
import com.yao.club.service.InformationService;
import com.yao.club.token.PassToken;
import com.yao.club.utils.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app/information")
@Api(value = "俱乐部资讯-资讯模块", description = "资讯模块", tags = "资讯模块")
public class InformationController {

    @Autowired
    private InformationService informationService;

    @PassToken
    @ApiOperation(value = "资讯类型列表", notes = "获取所有资讯类型")
    @GetMapping("/types")
    public ResponseEntity<List<InformationType>> getInformationTypes() {
        return informationService.getInformationTypes();
    }

    @PassToken
    @ApiOperation(value = "获取资讯列表", notes = "分页获取资讯列表，支持类型筛选和关键字搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeId", value = "资讯类型ID", required = false),
            @ApiImplicitParam(name = "keyword", value = "搜索关键词", required = false),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, defaultValue = "10")
    })
    @GetMapping("/pageList")
    public ResponseEntity<ResultEntity<InformationDetailVO>> getInformationList(
            @RequestParam(required = false) String typeId,
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
            
        if (pageNum < 1) {
            return ResponseUtils.failed("页码不能小于1");
        }
        if (pageSize < 1 || pageSize > 50) {
            return ResponseUtils.failed("每页数量必须在1-50之间");
        }
        
        return informationService.getInformationList(typeId, keyword, pageNum, pageSize);
    }

    @PassToken
    @ApiOperation(value = "获取资讯详情", notes = "根据资讯ID获取详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "资讯ID", required = true)
    })
    @GetMapping("/detail")
    public ResponseEntity<InformationDetailVO> getInformationDetail(@RequestParam String id) {
        if (StringUtils.isEmpty(id)) {
            return ResponseUtils.failed("资讯ID不能为空");
        }
        return informationService.getInformationDetail(id);
    }
}
